<?php

namespace Rblog\Module\Index\Controller;

class Index extends Common {
    
    private $_art;
    private $_sort;
    private $_comment;
    
    public function __construct() {
        parent::__construct();
        $this->_art = \Rexzy::model( 'Art' );
        $this->_sort = \Rexzy::model( 'Sort' );
        $this->_comment = \Rexzy::model( 'Comment' );
        $setup = new \Rblog\Lib\Syscache();
        $this->view->assign( array(
            'sorts' => $this->_sort->navSort( 0 ),
            'allsorts' => $this->_sort->getAllSort(),
            'newestCom' => $this->_comment->getNewest(),
            'sortUrl' => \Rexzy::url( 'sort/' ),
            'titleUrl' => \Rexzy::url( 'art/'),
            'webTitle' => $setup->get( 'title' ),
            'webDescription' => $setup->get( 'description' ),
            'webKeywords' => $setup->get( 'keywords' ),
        ));
    }
    
    /**
     * 博客前台主页
     */
    public function index() {
        $arts = $this->_art->index();
        $arts = $this->_comment->getEACNum( $arts );
        
        $this->view->assign( array(
            'arts' => $arts,
            'homepage' => 'index',
        ));
        $this->view->display();
    }
    
    /**
     * 显示某个分类的文章
     * 
     * @param string $sortSlugName 文章分类的简称
     */
    public function sort( $sortSlugName ) {
        $sortmesg = $this->_sort->getSortMesg( array( 'slugname' => $sortSlugName ) );
        $homepage = $this->_sort->findParent( $sortmesg['sortid'] ); 
        
        $allSorts = $this->_sort->getChildren( $sortmesg['sortid'] );
        $allSorts[] = $sortmesg['sortid'];
        $arts = $this->_art->sortArts( $allSorts );
        $arts = $this->_comment->getEACNum( $arts );
        
        $this->view->assign( array(
            'arts' => $arts,
            'homepage' => $homepage,
            'keyword' => "分类 " . $sortmesg['name'] . " 下的文章", 
        ));
        $this->view->display( 'Index/Index' );
    }
    
    /**
     * 阅读某篇文章
     * 
     * @param type $artName 查看文章的标题名（从rule中截取）
     */
    public function art( $title ) {
        $art = $this->_art->getArtMesg( array( 'title' => $title ) );
        
        if ( $art != false ) {
            $art = $this->_sort->getSortName( $art );

            $comments = $this->_comment->get( $art['artid'] );
            $totalComment = $this->_comment->gettotal( $art['artid'] );
            $this->view->assign( array(
                'noart' => false,
                'art' => $art,
                'comments' => $comments,
                'totalComment' => $totalComment,
                'webArtTitle' => "$title - ",
            ) );
            $this->view->display();
            
        } else {
            $this->view->assign( array(
                'noart' => true,
                'webArtTitle' => "$title - ",
                'title' => $title
            ) );
            $this->view->display();
        }
    }
    
    /**
     * 关键字搜索的文章展示
     */
    public function search() {
        $search = $this->request->input( 'search-content', 'p' );
        $arts = $this->_art->searchTitle( $search );
        $arts = $this->_comment->getEACNum( $arts );
        
        $this->view->assign( array(
            'arts' => $arts,
            'homepage' => 'index',
            'keyword' => "标题包含 '$search' 关键字的文章" 
        ) );
        $this->view->display( 'Index/Index');
    }
    
    /**
     * 搜索属于某个作者的所有文章
     */
    public function author( $author ) {
        $arts = $this->_art->author( $author );
        $arts = $this->_comment->getEACNum( $arts );
        
        $this->view->assign( array(
            'arts' => $arts,
            'homepage' => 'index',
            'keyword' => "作者 $author 的文章",
        ) );
        $this->view->display( 'Index/Index');
    }
    
    /**
     * 搜索属于某个时间的所有文章
     */
    public function date( $date ) {
        $arts = $this->_art->date( strtotime( $date ) );
        $arts = $this->_comment->getEACNum( $arts );
        
        $this->view->assign( array(
            'arts' => $arts,
            'homepage' => 'index',
            'keyword' => "$date 之前发布的文章",
        ) );
        $this->view->display( 'Index/Index');
    }

}
